#include <sys/socket.h>
#include <stdio.h>
#include <unistd.h>

#include "af.h"

int skfd = -1; /* generic raw socket desc.     */
extern struct aftype *aftypes[];

int sockets_open(int family)
{
	struct aftype **aft;
	int sfd = -1;
	static int force = -1;

	if (force < 0)
	{
		force = 0;

		if (access("/proc/net", R_OK))
			force = 1;
	}
	for (aft = aftypes; *aft; aft++)
	{
		struct aftype *af = *aft;
		int type = SOCK_DGRAM;
		if (af->af == AF_UNSPEC)
			continue;
		if (family && family != af->af)
			continue;
		if (af->fd != -1)
		{
			sfd = af->fd;
			continue;
		}
		/* Check some /proc file first to not stress kmod */
		if (!family && !force && af->flag_file)
		{
			if (access(af->flag_file, R_OK))
				continue;
		}

		af->fd = socket(af->af, type, 0);
		if (af->fd >= 0)
			sfd = af->fd;
	}
	if (sfd < 0)
		fprintf(stderr, ("No usable address families found.\n"));
	return sfd;
}
